# cronmon - Cron e-mail filtering and management
# Copyright (c) 2010 Crown copyright
# 
# This file is part of cronmon.
# 
# cronmon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# cronmon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with cronmon.  If not, see <http://www.gnu.org/licenses/>.

"""Escalate a message to RT"""

from django.conf import settings
from django.contrib.sites.models import Site
import re

import cookielib
import urllib
import urllib2
import urlparse

from cronmon.rt import RT

def escalate(message):
    rt = RT(base_url=settings.RT_BASE_URL, username=settings.RT_USERNAME, password=settings.RT_PASSWORD)
    ticket = rt.new_ticket(
        queue=settings.RT_QUEUE,
        subject='(from cronmon) %s' % message,
        text=message.text_body(),
        requestor=settings.RT_REQUESTOR,
#        refers_to='http://%s%s' % (Site.objects.get_current().domain, message.get_absolute_url())
    )

    ticket.save()

    message.status = 'E'
    message.ticket = ticket.id
    message.save()
    message.add_log_message('Escalated to RT by administrator', type='escalation')
